const { Controller } = require('egg');

class orderController extends Controller {
  // 获取
  async index() {
    try {
      let query = this.ctx.request.query;
      let orderData = await this.ctx.service.order.getOrder(query);
      this.ctx.body = {
        code: 20000,
        message: '获取商品成功',
        data: orderData
      };
    } catch (error) {
      this.ctx.body = {
        code: 50000,
        message: '服务器错误'
      };
    }
  }
  // 修改
  async update() {
    try {
      let id = this.ctx.params.id;
      let { newAddress, newSendState } = this.ctx.request.body;
      // 根据传入的新参数判断执行哪个数据库来操作
      let rel;
      if (newAddress) {
        rel = await this.ctx.service.order.changeOrderAddr(id, newAddress);
      }
      if (!isNaN(newSendState)){
        rel = await this.ctx.service.order.changeOrderState(id, newSendState);
      }
      console.log(rel);
      this.ctx.body = {
        code: 20000,
        message: '修改商品成功'
      };
    } catch (error) {
      this.ctx.body = {
        code: 50000,
        message: '服务器错误'
      };
    }
  }
  // 搜索
  async searchOrderList() {
    let query = this.ctx.request.query;
    console.log(query);
    let rel = await this.ctx.service.order.searchOrder(query);
    this.ctx.body = {
      code: 20000,
      message: '获取数据成功',
      data: rel
    };
  }
//  获取订单数量
  async getOrderCount() {
    let list = await this.ctx.service.order.getOrderNum();
    // console.log(list.length);
    this.ctx.body = {
      code: 20000,
      message: '获取成功',
      data: list.length
    }
  }
}

module.exports = orderController;
